/// @tags: Bitset DAG
#include <bitset>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

namespace BlueQuantum {

int const N = 6e4 + 5, M = 1e5 + 5;

int n, m, e_cnt;
int deg[N], cdeg[N], heads[N];
long long ans;
queue<int> q;
bitset<N / 2> bs[N];

struct Edge {
  int v, nxt;
} e[M];

inline void add(int u, int v) {
  e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt;
}

inline int main() {
  cin >> n >> m;
  ans = -m;
  for (int i = 1, u, v; i <= m; ++i) {
    cin >> u >> v;
    add(v, u), ++deg[u];
  }
  int const lim = n >> 1;
  for (int i = 1; i <= n; ++i)
    if (!deg[i]) q.push(i);
  memcpy(cdeg, deg, sizeof(cdeg));
  while (!q.empty()) {
    int u = q.front();
    ans += bs[u].count();
    if (u <= lim) bs[u][u] = true;
    q.pop();
    for (int i = heads[u], v; i; i = e[i].nxt) {
      bs[v = e[i].v] |= bs[u];
      if (!(--cdeg[v])) q.push(v);
    }
  }
  for (int i = 1; i <= n; ++i) {
    bs[i].reset();
    if (!deg[i]) q.push(i);
  }
  memcpy(cdeg, deg, sizeof(cdeg));
  while (!q.empty()) {
    int u = q.front();
    ans += bs[u].count();
    if (u > lim) bs[u][u - lim] = true;
    q.pop();
    for (int i = heads[u], v; i; i = e[i].nxt) {
      bs[v = e[i].v] |= bs[u];
      if (!(--cdeg[v])) q.push(v);
    }
  }
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("world.in", "r", stdin);
  freopen("world.out", "w", stdout);
#endif
#endif

  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
